﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;
using System.Collections.Specialized;
using System.Data;

namespace CP.Repositories.GPM
{
    public class ButtonRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<Button_List> GetAllButtons(int webpartid)
        {
            return db.ExecuteObject<Button_List>(string.Format(@"select * from Button_list 
                                where webpart_id = {0} order by Button_id", webpartid));
        }

        public IList<string> SelectButtonsByUserID(string userid, string formname)
        {
            return db.ExecuteSimpleTypeList<string>(string.Format(@"SELECT   DISTINCT T2.ACTION_ID
                                  FROM   user_group_relation t1, WEBPART_RIGHT_BUTTON t2, webpart t3
                                 WHERE       T1.GROUP_ID = T2.GROUP_ID
                                         AND T2.WEBPART_ID = T3.WEBPART_ID
                                         AND T1.USER_ID = '{0}'
                                         AND T3.FORM_NAME = '{1}'", userid, formname));
        }

        public Button_List GetButtonByID(int webpartid, int buttonid)
        {
            IList<Button_List> Buttons = db.ExecuteObject<Button_List>(string.Format(@"select * 
                                                from Button_list where webpart_id = {0}
                                                and Button_id = {1} ", webpartid, buttonid));
            if (Buttons.Count > 0)
                return Buttons[0];
            return null;
        }

        public bool DeleteButton(int webpartid, IList<int> ids)
        {
            try
            {
                db.BeginTransaction();
                foreach (int id in ids)
                {
                    db.ExecuteNonQuery(string.Format(@"delete Button_list 
                                        where webpart_id = {0}
                                          and Button_id = {1} ", webpartid, id));
                    db.ExecuteNonQuery(string.Format(@"delete WEBPART_RIGHT_BUTTON 
                                        where webpart_id = {0}
                                          and Button_id = {1} ", webpartid, id));
                }
                db.Commit();
                return true;
            }
            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }

        public bool DeleteButtonByID(int webpartid, int btnid)
        {
            db.ExecuteNonQuery(string.Format(@"delete Button_list 
                                        where webpart_id = {0}
                                          and Button_id = {1} ", webpartid, btnid));
            db.ExecuteNonQuery(string.Format(@"delete WEBPART_RIGHT_BUTTON 
                                        where webpart_id = {0}
                                          and Button_id = {1} ", webpartid, btnid));
            return true;
        }

        public bool DeleteButtonByWebpartID(int webpartid)
        {
            db.ExecuteNonQuery(string.Format(@"delete Button_list 
                                        where webpart_id = {0} ", webpartid));
            db.ExecuteNonQuery(string.Format(@"delete WEBPART_RIGHT_BUTTON 
                                        where webpart_id = {0} ", webpartid));
            return true;
        }

        public bool AddButton(int webpartid, Button_List button)
        {
            if (ButtonNameExists(webpartid, button.action_id))
                throw new Exception("按钮Name已经存在 webpart id=[" + webpartid + "] button name =[" + button.action_id + "]");
            button.button_id = GetButtonIDBySeq();
            db.ExecuteNonQuery(string.Format(@"insert into Button_list(Button_id,action_id,
                                        button_name,webpart_id) 
                                        values({0},'{1}','{2}',{3}) ",
                                button.button_id, button.action_id, button.button_name,
                                webpartid));
            return true;
        }

        public bool ButtonNameExists(int webpartid, string buttonname)
        {
            return db.ExecuteScalar<int>(string.Format(@"select count(*) from dual where 
                                            exists (select null from button_list where
                                                    webpart_id = {0} and action_id = '{1}')",
                      webpartid, buttonname)) == 0 ? false : true;
        }

        public bool ButtonNameExists2(int webpartid, int buttonid, string buttonname)
        {
            return db.ExecuteScalar<int>(string.Format(@"select count(*) from dual where 
                                            exists (select null from button_list where
                                                    webpart_id = {0} and action_id = '{1}' 
                                                    and button_id <> {2})",
                      webpartid, buttonname, buttonid)) == 0 ? false : true;
        }

        public bool UpdateButton(int webpartid, Button_List button)
        {
            int ret = db.ExecuteScalar<int>(string.Format(@"select count(*) from dual
                                           where exists(select null from 
                                           button_list where webpart_id = {0}
                                                    and button_id = {1} )",
                                          webpartid, button.button_id));
            if (0 == ret)
                throw new Exception("按钮编号不存在");

            if (ButtonNameExists2(webpartid, button.button_id, button.action_id))
                throw new Exception("按钮Name已经存在");

            db.ExecuteNonQuery(string.Format(@"update Button_list set button_name = '{1}',
                                            action_id = '{2}', post_url = '{4}'  
                                            where webpart_id = {3} and button_id = {0} ",
                               button.button_id, button.button_name, button.action_id,
                               webpartid, button.post_url));
            return true;
        }

        public int GetButtonIDBySeq()
        {
            return SequenceGenerator.GetSeqNextValue("button_seq");
        }
    }
}
